﻿using System;
using System.Drawing;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.WindowsDesktopEx.Winform;

namespace WinFormApp
{
    public partial class FTestNotifyManager : Form
    {
        public FTestNotifyManager()
        {
            InitializeComponent();
        }

        private void FTestNotifyManager_Load(object sender, EventArgs e)
        {
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            try
            {
                logControl1.AddLog(string.Format("{0} {1}", DateTime.Now, e.Item.Content), e.Item.Level);
            }
            catch (Exception ex)
            {
                logControl1.AddLog(ex.Message, LogLevel.Error);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            logControl1.Clear();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            NotifyManager.Default.Notify(new NotifyItem()
            {
                Text = $"{TimeEx.GetTimestamp()} 这是OK信息,啦啦啦.",
                BackgroundColor = Color.Gray,
                MesageImage = NotifyManager.OKImg
            });
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            NotifyManager.Default.Notify(new NotifyItem()
            {
                Text = $"{TimeEx.GetTimestamp()} 这是info信息,啦啦啦.",
                BackgroundColor = Color.Green,
                MesageImage = NotifyManager.InfoImg,
                //Duration = 20000,
                Tag = new Person(TimeEx.GetTimestamp(), "这真的是info信息"),
                MouseClick = this.InfoMouseClick,
                Size = new Size(400, 50),
            });
        }

        private void InfoMouseClick(NotifyItem item)
        {
            MessageBox.Show(((Person)item.Tag).Name);
        }

        private void btnWarn_Click(object sender, EventArgs e)
        {
            NotifyManager.Default.Notify(new NotifyItem()
            {
                Text = $"{TimeEx.GetTimestamp()} 这是Warn信息,啦啦啦.",
                BackgroundColor = Color.Goldenrod,
                MesageImage = NotifyManager.WarnImg
            });
        }

        private void btnError_Click(object sender, EventArgs e)
        {
            NotifyManager.Default.Notify(new NotifyItem()
            {
                Text = $"{TimeEx.GetTimestamp()} 这是Error信息,啦啦啦.",
                BackgroundColor = Color.Brown,
                MesageImage = NotifyManager.ErrorImg
            });
        }

        private void btnRing_Click(object sender, EventArgs e)
        {
            NotifyManager.Default.Notify(new NotifyItem()
            {
                Text = $"{TimeEx.GetTimestamp()} 这是Ring信息,啦啦啦.",
                BackgroundColor = Color.HotPink,
                MesageImage = NotifyManager.RingImg
            });
        }

        private class Person
        {
            public long Id { get; set; }

            public string Name { get; set; }

            public Person(long id, string name)
            {
                this.Id = id;
                this.Name = name;
            }
        }
    }
}
